package com.zg.nacos.domain.bo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/6/27 16:34
 */

@Data
public class InstanceInfo {
    private String namespace;
    private String serviceName;
    private String serviceCode;
    private String ip;
    private int port;
    private String healthCheckUrl;
    private String httpHealthCheckUrl;

    private HealthStatus healthStatus = HealthStatus.HEALTHY;
    private int failureCount = 0;
    private long unhealthyTime; // 标记为不健康的时间戳

    public String getHttpHealthCheckUrl(){
        if(this.healthCheckUrl.startsWith("http")){
            return  healthCheckUrl;
        }
        String url= "http://"+ip+":"+port;
        if(this.healthCheckUrl.startsWith("/")){
            return   url+this.healthCheckUrl;
        }else {
            return   url+"/"+this.healthCheckUrl;
        }
    }
    public enum HealthStatus {
        HEALTHY,     // 健康
        SUSPICIOUS,  // 可疑
        UNHEALTHY    // 不健康
    }

    // 增加失败计数
    public void incrementFailureCount() {
        this.failureCount++;
    }

    public void markSuspicious() {
        this.healthStatus = HealthStatus.SUSPICIOUS;
    }

    public void markUnhealthy() {
        this.healthStatus = HealthStatus.UNHEALTHY;
        this.unhealthyTime = System.currentTimeMillis();
    }

    public void markHealthy() {
        this.healthStatus = HealthStatus.HEALTHY;
        this.failureCount = 0;
    }
}